package com.wiretap.action.admin.manage;

import java.io.File;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.wiretap.action.WiretapActionSupport;
import com.wiretap.po.Contact;
import com.wiretap.service.ContactService;
import com.wiretap.util.CommenUtil;
import com.wiretap.util.Constants;
import com.wiretap.util.ContactAnalyzeUtil;
import com.wiretap.util.DateUtils;
import com.wiretap.util.Pager;

public class ContactManageAction extends WiretapActionSupport {

	private static final long serialVersionUID = 3098610836187798119L;
	private static final String FILE_SAVE_DIR = "file.saveDir";
	private static final String FILE_CONFIG_NAME = "fileupload";
	private String phone;
	private int cPage;
	private ContactService contactService;
	private Pager<Contact> contacts;
	private String cName;
	private String cNumber;
	private File upload;
	private String uploadFileName;
	private String uploadContentType;

	/**
	 * 查询所有通讯录列表
	 */
	@Override
	public String execute() throws Exception {
		cPage = cPage < 1 ? 1 : cPage;
		if (!CommenUtil.isEmpty(phone)) {
			contacts = contactService.getAllContactByPhone(phone, cPage, Constants.DEFAULT_PAGESIEZE);
		}
		return SUCCESS;
	}

	public void addContact() throws Exception {
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		if (CommenUtil.isEmpty(cName) || CommenUtil.isEmpty(cNumber) || CommenUtil.isEmpty(phone)) {
			out.print("error");
		} else {
			contactService.addContact(cName, cNumber, phone);
			out.print("success");
		}
		out.flush();
		out.close();
	}

	/**
	 * 解析联系人文件
	 * 
	 * @throws Exception
	 */
	public void uploadContactFile() throws Exception {
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		Map<String, Object> result = new HashMap<String, Object>();
		if (upload == null || uploadContentType == null || uploadFileName == null) {
			result.put("result", false);
		} else {
			// 获取文件配置
			ResourceBundle rb = ResourceBundle.getBundle(FILE_CONFIG_NAME);
			// 获取文件上传保存路径配置
			String saveDir = rb.getString(FILE_SAVE_DIR);
			// 获取保存路径
			String uploadDir = ServletActionContext.getServletContext().getRealPath(saveDir);
			// 创建路径
			File directory = new File(uploadDir);
			// 临时文件名称
			String tmpUploadimgFileName = DateUtils.getDateString(new Date(), Constants.DATEFORMAT_YYYY_MM_DD_HH_MM_SS_NO_SEPARATOR);
			// 重新设置文件名称
			uploadFileName = tmpUploadimgFileName + uploadFileName.substring(uploadFileName.indexOf("."));
			// 路径不存在时
			if (!directory.exists()) {
				// 创建不存在路径
				directory.mkdirs();
			}
			// 新建需要保存的新文件
			File dest = new File(uploadDir + "/" + uploadFileName);
			// 将上传文件保存至配置路径
			upload.renameTo(dest);
			String contacts = ContactAnalyzeUtil.analyze(dest);
			result.put("result", true);
			result.put("contact", contacts);
			dest.delete();
		}
		String str = new Gson().toJson(result);
		out.print(str);
		out.flush();
		out.close();
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public ContactService getContactService() {
		return contactService;
	}

	public void setContactService(ContactService contactService) {
		this.contactService = contactService;
	}

	public Pager<Contact> getContacts() {
		return contacts;
	}

	public void setContacts(Pager<Contact> contacts) {
		this.contacts = contacts;
	}

	public int getcPage() {
		return cPage;
	}

	public void setcPage(int cPage) {
		this.cPage = cPage;
	}

	public String getcName() {
		return cName;
	}

	public void setcName(String cName) {
		this.cName = cName;
	}

	public String getcNumber() {
		return cNumber;
	}

	public void setcNumber(String cNumber) {
		this.cNumber = cNumber;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

}
